#include <SoftwareSerial.h>

#include "TinyGPS.h"
TinyGPS gps;
SoftwareSerial nss(10,11);


// this constant won't change:
const int pinTX = 2;    // Pino do sinal audio
const int soundPin = 3;    // Pino do sinal audio CW
const int freqtone = 780;    // Ton do sinal
const int duraDi = 80;    // Duração do Di
const int duraDa = 180;    // duraçãoo do Da
const int interTon = 150;    // Intervalo entre notas
const int interLetra = 400;    // Intervalo entre letras

long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;

// constante principal 
String txtMsg = "PP1BRE "; //mensagem a ser enviada


void setup(void)
{   
  Serial.begin(9600); 
  
  pinMode(3, OUTPUT);  
}

 void loop() {
   int x;
   nss.begin(9600);
     while (nss.available())
  {
    int c = nss.read();
    Serial.print(char(c));
    if (gps.encode(c))
    { 
   nss.end();
      txtMsg = "PP1BRE ";
      // retrieves +/- lat/long in 100000ths of a degree
      gps.get_position(&lat, &lon, &fix_age);

      txtMsg = txtMsg + String(lat);
      txtMsg = txtMsg + String(lon);
      Serial.println("");
      Serial.println("--------------------- decodificou --------------------");
        
      for (x=0;x<=txtMsg.length();x++){
        rtty_50(txtMsg.charAt(x));
        Serial.println(txtMsg.charAt(x));
      }
      
        
        //Serial.println(lat,' - ',lon,' - ',fix_age);
        // time in hhmmsscc, date in ddmmyy
        gps.get_datetime(&date, &time, &fix_age);
        Serial.println("date : ");
        Serial.print(date, DEC);
        
        // returns speed in 100ths of a knot
        //speed = gps.speed();
        //Serial.println("Speed : ");
        //Serial.print(speed, DEC);
        
        // course in 100ths of a degree
        //course = gps.course();
        //Serial.println("course :");
        //Serial.println(course, DEC);
    }
  }
 }

  
////   
  //nss.end();
  /*
  inicioTX();  
  morse();
  fimTX();
  delay(10000);
  */

void morse (void){
  int x;
  for (x=0;x<=sizeof(txtMsg);x++){
      switch (txtMsg[x]) {
        case 'a': tocar(1);tocar(2);delay(interLetra); break;
        case 'b': tocar(2);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case 'c': tocar(2);tocar(1);tocar(2);tocar(1);delay(interLetra); break;
        case 'd': tocar(2);tocar(1);tocar(1);delay(interLetra); break;
        case 'e': tocar(1);delay(interLetra); break;
        case 'f': tocar(1);tocar(1);tocar(2);tocar(1);delay(interLetra); break; 	
        case 'g': tocar(2);tocar(2);tocar(1);delay(interLetra); break;
        case 'h': tocar(1);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case 'i': tocar(1);tocar(1);delay(interLetra); break;
        case 'j': tocar(1);tocar(2);tocar(2);tocar(2);delay(interLetra); break;
        case 'w': tocar(1);tocar(2);tocar(2);delay(interLetra); break;
        case 'k': tocar(2);tocar(1);tocar(2);delay(interLetra); break;
        case 'l': tocar(1);tocar(2);tocar(1);tocar(1);delay(interLetra); break;
        case 'm': tocar(2);tocar(2);delay(interLetra); break;
        case 'n': tocar(2);tocar(1);delay(interLetra); break;
        case 'o': tocar(2);tocar(2);tocar(2);delay(interLetra); break;
        case 'p': tocar(1);tocar(2);tocar(2);tocar(1);delay(interLetra); break;
        case 'q': tocar(2);tocar(2);tocar(1);tocar(2);delay(interLetra); break;
        case 'r': tocar(1);tocar(2);tocar(1);delay(interLetra); break;
        case 's': tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case 't': tocar(2);delay(interLetra); break;
        case 'u': tocar(1);tocar(1);tocar(2);delay(interLetra); break;
        case 'v': tocar(1);tocar(1);tocar(1);tocar(2);delay(interLetra); break;
        case 'x': tocar(2);tocar(1);tocar(1);tocar(2);delay(interLetra); break;
        case 'y': tocar(2);tocar(1);tocar(2);tocar(2);delay(interLetra); break;
        case 'z': tocar(2);tocar(2);tocar(1);tocar(1);delay(interLetra); break;
        case '1': tocar(1);tocar(2);tocar(2);tocar(2);tocar(2);delay(interLetra); break;
        case '2': tocar(1);tocar(1);tocar(2);tocar(2);tocar(2);delay(interLetra); break;
        case '3': tocar(1);tocar(1);tocar(1);tocar(2);tocar(2);delay(interLetra); break;
        case '4': tocar(1);tocar(1);tocar(1);tocar(1);tocar(2);delay(interLetra); break;
        case '5': tocar(1);tocar(1);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case '6': tocar(2);tocar(1);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case '7': tocar(2);tocar(2);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case '8': tocar(2);tocar(2);tocar(2);tocar(1);tocar(1);delay(interLetra); break;
        case '9': tocar(2);tocar(2);tocar(2);tocar(2);tocar(1);delay(interLetra); break;
        case '0': tocar(2);tocar(2);tocar(2);tocar(2);tocar(2);delay(interLetra); break;
        case '.': tocar(1);tocar(2);tocar(1);tocar(2);tocar(1);tocar(2);delay(interLetra); break;
        case ',': tocar(2);tocar(2);tocar(1);tocar(1);tocar(2);tocar(2);delay(interLetra); break;
        case '?': tocar(1);tocar(1);tocar(2);tocar(2);tocar(1);tocar(1);delay(interLetra); break;
        case '!': tocar(2);tocar(1);tocar(2);tocar(1);tocar(2);tocar(2);delay(interLetra); break;
        case '/': tocar(2);tocar(1);tocar(1);tocar(2);tocar(1);delay(interLetra); break;
        case '(': tocar(2);tocar(1);tocar(2);tocar(2);tocar(1);delay(interLetra); break;
        case ')': tocar(2);tocar(1);tocar(2);tocar(2);tocar(1);tocar(2);delay(interLetra); break;
        case '&': tocar(1);tocar(2);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case ':': tocar(2);tocar(2);tocar(2);tocar(2);tocar(1);tocar(1);tocar(1);delay(interLetra); break;
        case ';': tocar(2);tocar(1);tocar(2);tocar(1);tocar(2);tocar(1);delay(interLetra); break;
        case '=': tocar(2);tocar(1);tocar(1);tocar(1);tocar(2);delay(interLetra); break;
        case ' ': delay(interLetra);delay(interLetra);break;
        case '_': tocar(1);tocar(1);tocar(2);tocar(2);tocar(1);tocar(2);delay(interLetra); break;
        case '"': tocar(1);tocar(2);tocar(1);tocar(1);tocar(2);tocar(1);delay(interLetra); break;
        case '$': tocar(1);tocar(1);tocar(1);tocar(2);tocar(1);tocar(1);tocar(2);delay(interLetra); break;
        case '@': tocar(1);tocar(2);tocar(2);tocar(1);tocar(2);tocar(1);delay(interLetra); break;
      }
  }
}

void tocar(int x) {
  int dur = duraDa;
    if ( x == 1){ dur = duraDi;}
  tone(soundPin,freqtone,dur);
  delay(dur);
  delay(interTon);
}

void inicioTX () {
  digitalWrite(pinTX, HIGH);   // set the LED on
  delay (200);
}

void fimTX () {
  delay (200);
  digitalWrite(pinTX, LOW);    // set the LED off
}

void rtty_50(char abit) { //This is the RTTY on pin 5 with 50 baud transmition string 
  
  digitalWrite(3, LOW); // START BIT set the radio to off 
  delay(20); // wait for 20ms 
  
    for(int k = 0; k<8; k++){ //This is a loop as there are 8 bits in a byte, which is one charactor 
      if (abit & 1){ // This will return 1 if the last bit is 1, or 0 if the last bit is 0, as 00000001 AND abcdefgh = 0000000h 
        digitalWrite(3, HIGH); //HIGH BIT set the radio to on 
        delay(20); // wait for 20ms 
        } 
      else{ 
        digitalWrite(3, LOW); // Low BIT set the radio to off 
        delay(20); // wait for 20ms 
        } 
    abit = abit >>1; //Shift bits to the right by 1 step 
     }
    digitalWrite(3, HIGH); // END BIT set the radio to on 
    delay(20); // wait for 20ms 
}






